/*
 * test.c
 *
 *  Created on: Mar 30, 2011
 *      Author: shaican
 */
#include "types.h"
#include "stat.h"
#include "user.h"

void stam(){
	printf(1,"a thread of a thread\n");
	int a = 9;
	thread_exit(&a);
}

void run(){
	void** ret_val= 0;
	printf(1,"testing thread function\n");
	printf(1, "thread id is %d\n", thread_getid());
	printf(1, "Process id is %d\n", thread_getProcId());
	int* stack1 = malloc(4096);
	int id = thread_create((void(*))stam, stack1, 4096);
	thread_join(id, ret_val);
	printf(1, "The result of the 2nd thread is %d\n", *(int*)(*ret_val));
	int num = 5;
	thread_exit(&num);
}

int main(void){

	void** ret_val= 0;
	int* stack = malloc(4096);
	int thread_id = thread_create((void(*))run, stack, 4096);
	int res = thread_join(thread_id, ret_val);
	printf(1, "The result of join is %d\n", res);
	printf(1, "The result of the 1st thread is %d\n", *(int*)(*ret_val));
	free(stack);
	exit();
	return 1;
}
